package com.yhb.controller.admin;

import com.yhb.conf.ResultCode;
import com.yhb.controller.admin.form.QueryBonusForm;
import com.yhb.controller.admin.form.QueryPosBoundForm;
import com.yhb.model.MPosSku;
import com.yhb.model.User;
import com.yhb.service.MPosService;
import com.yhb.service.UserService;
import lombok.extern.log4j.Log4j;
import me.edzh.spring.bootstrap.annotations.CurrentUser;
import me.edzh.spring.bootstrap.annotations.NeedAuth;
import me.edzh.spring.bootstrap.controller.BaseController;
import me.edzh.spring.bootstrap.model.Response;
import me.edzh.spring.bootstrap.utils.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/admin/pos")
@RestController


public class AdminPosController extends BaseController {

    @Autowired
    private MPosService mPosService;

    @Autowired
    private UserService userService;

    @PostMapping("/sku/import")
    @NeedAuth(User.A_ADMIN)
    public Response importPosSku(@RequestBody List<MPosSku> skus) {
        mPosService.importSku(skus);
        return Response.ok();
    }

    @PostMapping("/bound")
    @NeedAuth(User.A_ADMIN)
    public Response boundPos(@RequestBody QueryPosBoundForm boundForm){
         User user = userService.getUserByMobilePhone(boundForm.getMobile());
         if(user == null){
              throw new ServiceException(ResultCode.ERROR_TOAST_ERR, "用户不存在");
         }
         mPosService.addBound(boundForm.getPosManufacturerId(),boundForm.getSn(),user.getId());
         return Response.ok();
    }

    @PostMapping("/unbound")
    @NeedAuth(User.A_ADMIN)
    public Response unBound(@RequestBody QueryPosBoundForm boundForm, @CurrentUser User user){
        mPosService.unBound(boundForm.getBoundId(),user);
        return Response.ok();
    }

    @PostMapping("/query")
    @NeedAuth(User.A_ADMIN)
    public Response query(@RequestBody QueryPosBoundForm boundForm){

        return Response.ok(mPosService.search(boundForm));
    }

}



